package com.dailybule.java.mybatis.m2;

import com.dailybule.java.mybatis.m2.bean.Dept;
import com.dailybule.java.mybatis.m2.bean.Emp;
import com.dailybule.java.mybatis.m2.mapper.DeptMapper;
import com.dailybule.java.mybatis.m2.mapper.EmpMapper;
import lombok.SneakyThrows;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class Main {

    @SneakyThrows
    private static void a(){
        // 1. 加载配置文件
        InputStream in = Resources.getResourceAsStream("config.xml");
        // 2. 产生会话工厂
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        // 3. 产生会话
        SqlSession session = ssf.openSession(true);
        // 4. 获取到mapper的`对象`
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        // 5. 调用方法
        List<Emp> list = mapper.find1();
        list.forEach(System.out::println);
        // 7. 关闭连接
        session.close();
    }

    @SneakyThrows
    private static void b(){
        // 1. 加载配置文件
        InputStream in = Resources.getResourceAsStream("config.xml");
        // 2. 产生会话工厂
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        // 3. 产生会话
        SqlSession session = ssf.openSession(true);
        // 4. 获取到mapper的`对象`
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        // 5. 调用方法
        List<Emp> list = mapper.find2();
        list.forEach(System.out::println);
        // 7. 关闭连接
        session.close();
    }

    @SneakyThrows
    private static void c(){
        // 1. 加载配置文件
        InputStream in = Resources.getResourceAsStream("config.xml");
        // 2. 产生会话工厂
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        // 3. 产生会话
        SqlSession session = ssf.openSession(true);
        // 4. 获取到mapper的`对象`
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        // 5. 调用方法
        List<Emp> list = mapper.find3();
        list.forEach(System.out::println);
        // 7. 关闭连接
        session.close();
    }

    @SneakyThrows
    private static void d(){
        // 1. 加载配置文件
        InputStream in = Resources.getResourceAsStream("config.xml");
        // 2. 产生会话工厂
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        // 3. 产生会话
        SqlSession session = ssf.openSession(true);
        // 4. 获取到mapper的`对象`
        DeptMapper mapper = session.getMapper(DeptMapper.class);
        // 5. 调用方法
        List<Dept> list = mapper.findDept();
        list.forEach(System.out::println);
        // 7. 关闭连接
        session.close();
    }
    public static void main(String[] args) {
        d();
    }
}